package com.carefree.system.modules.admin.service.impl;

import com.alibaba.fastjson2.JSONObject;
import com.alibaba.fastjson2.TypeReference;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.carefree.common.core.PageUtils;
import com.carefree.common.core.QueryParam;
import com.carefree.system.common.utils.AliyunOssUtils;
import com.carefree.system.common.utils.SecurityUtils;
import com.carefree.system.modules.admin.dao.OssDao;
import com.carefree.system.modules.admin.entity.OssEntity;
import com.carefree.system.modules.admin.service.OssService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * 文件上传
 *
 * @author tengbin
 */
@Service("ossService")
public class OssServiceImpl extends ServiceImpl<OssDao, OssEntity> implements OssService {

    @Resource
    private SecurityUtils securityUtils;

    @Resource
    private AliyunOssUtils aliyunOssUtils;


    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<OssEntity> page = page(new QueryParam<OssEntity>().getPage(params), new QueryWrapper<OssEntity>());
        return new PageUtils(page);
    }


    /**
     * 保存文件上传记录
     *
     * @param fileUrl
     * @return
     */
    @Override
    public boolean saveData(String url) {
        if (StringUtils.isNotBlank(url)) {
            OssEntity oss = JSONObject.parseObject(url, new TypeReference<OssEntity>() {
            });
            oss.setCreator(securityUtils.getPrincipalForUserId());
            return save(oss);
        }
        return false;
    }


    /**
     * 已上传成功的图片url地址
     *
     * @param urls 图片url地址数组
     * @return
     */
    @Override
    public boolean saveDatas(String[] urls) {
        List<OssEntity> ossEntityList = Arrays.asList(urls).stream().map(url -> {
            OssEntity ossEntity = new OssEntity();
            ossEntity.setUrl(url);
            ossEntity.setCreator(securityUtils.getPrincipalForUserId());
            return ossEntity;
        }).collect(Collectors.toList());
        return saveBatch(ossEntityList);
    }

    /**
     * 删除一个或多个文件记录
     *
     * @param ids 文件id
     * @return
     */
    @Override
    public boolean deleteImagesByIds(List<Long> ids) {
        // 查询所有符合ids的记录
        List<OssEntity> ossEntityList = list(new QueryWrapper<OssEntity>().in("id", ids));

        if (! CollectionUtils.isEmpty(ossEntityList)) {
            // 将查询到的记录中的url属性值遍历截取
            List<String> urls = ossEntityList.stream().map(ossEntity -> {
                String url = ossEntity.getUrl();
                return url.substring(url.lastIndexOf(".com/") + 5, url.length());
            }).collect(Collectors.toList());

            // 1.执行数据库记录删除
            removeByIds(ids);
            // 2.执行阿里云oss服务器中的记录删除
            return aliyunOssUtils.deleteFiles(urls, "");
        }
        return false;
    }
}
